package m.jclecai.com.adapter.holder;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.bean.BankBean;
import m.jclecai.com.utils.BitmapUtil;
import m.jclecai.com.utils.StringUtils;


/**
 * Created by ZD on 2017/6/8.
 */

public class BankListHolder extends ViewHolder {



    @BindView(R.id.tv_bankname)
    TextView mTvBankname;
    @BindView(R.id.tv_banknumber)
    TextView mTvBanknumber;
    @BindView(R.id.cb_default)
    CheckBox mCbDefault;

    @BindView(R.id.tv_del)
    TextView mTvDel;


    @BindView(R.id.iv_bank_icon)
    ImageView mIvBankIcon;

    private BankBean bankBean;


    public BankListHolder(Context context, @LayoutRes int resource) {
        super(context, resource);
    }


    public void init( final BankBean bankBean, String[] banks, boolean showCheck) {
        this.bankBean = bankBean;
        mCbDefault.setChecked(false);
        mCbDefault.setChecked(bankBean.isCheck());
        //银行名字；
        mTvBankname.setText(bankBean.getBank());
        String bankNumber="";
/*
        for (int i = 0; i < bankBean.getBankNumber().length()-4; i++) {
            bankNumber+="*";
        }*/
        bankNumber+= StringUtils.formatBank(bankBean.getBankNumber());

        //银行卡号
        mTvBanknumber.setText(bankNumber);
        showICon(banks, bankBean.getBank());

        mTvDel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //暴露点击事件
                if (mOnClickDelListener != null) {
                    mOnClickDelListener.onClickDel();
                }
            }
        });
        mCbDefault.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickDelListener != null) {
                    mOnClickDelListener.onClickSetDefault(mCbDefault,mCbDefault.isChecked());
                }
            }
        });

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mCbDefault.toggle();
                if (mOnClickDelListener != null) {
                    mOnClickDelListener.onClickSetDefault(mCbDefault,mCbDefault.isChecked());
                }
            }
        });

    }

    void showICon(String[] banks, String bank) {
        mIvBankIcon.setImageDrawable(ContextCompat.getDrawable(getContext(), R.color.transparent));
        for (int i = 0; i < banks.length; i++) {
            if (banks[i].equals(bank)) {
                mIvBankIcon.setImageDrawable(BitmapUtil.getDrawableByName(getContext(), "b" + i));
                break;
            }
        }
    }

    public interface OnClickDelListener{
        void onClickDel();
        void onClickSetDefault(View view, boolean isChecked);
    }

    public OnClickDelListener mOnClickDelListener;

    public void setOnClickDelListener(OnClickDelListener onClickDelListener) {
        mOnClickDelListener = onClickDelListener;
    }
    /*    @BindView(R.id.iv_icon)
    ImageView ivIcon;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.cb_del)
    CheckBox cb_del;
    BankBean bankBean;

    public BankListHolder(Context context, @LayoutRes int resource) {
        super(context, resource);
    }

    public void init(BankBean bankBean, String[] banks, boolean showCheck) {
        this.bankBean = bankBean;
        cb_del.setVisibility(showCheck ? VISIBLE : INVISIBLE);
        cb_del.setChecked(bankBean.isBind());
        tvName.setText("");
        tvName.append(bankBean.getBank());
        tvName.append("  (尾号:" + StringUtils.formatBank(bankBean.getBankNumber()) + ")");
        showICon(banks, bankBean.getBank());
    }

    void showICon(String[] banks, String bank) {
        ivIcon.setImageDrawable(ContextCompat.getDrawable(getContext(), R.color.transparent));
        for (int i = 0; i < banks.length; i++) {
            if (banks[i].equals(bank)) {
                ivIcon.setImageDrawable(BitmapUtil.getDrawableByName(getContext(), "b" + i));
                break;
            }
        }
    }

    @OnCheckedChanged(R.id.cb_del)
    void check(){
        this.bankBean.setBind(cb_del.isChecked());
    }*/
}
